<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>

<%
	request.setAttribute("title","货品信息");
	request.setAttribute("modelConfigPath", "cargo");
%>

<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<style type="text/css">
.hot-container{
overflow: auto;
-webkit-box-shadow: 0 0 0 0; 
 box-shadow: 0 0 0 0;
}
body{
	background-color:#fff;

}
.colHeader{
word-wrap:break-word;white-space:normal
}
td{
word-break:break-all;
}
.handsontable .htCore{
	border-bottom: 1px solid #ddd;
}
.panel-body1 {
    padding: 0px !important;
}

</style>
	
<t:panel cssClass="panel-flat form-detailed" customBody="true" customHead="true">
	<t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<div style="float:left;margin-top:13px;margin-right:20px;"><span style="color:red;" class="aaa"></span></div>
		<div style="float:right; "><t:button icon="icon-floppy-disk" onclick="saveForm()" cssClass="btn-info" csize="sm">保存</t:button></div>
	</t:panelFoot>	

	<div class="panel-body1">
		<div class="hot-container">
			<div id="hot_parameter_info"></div>
		</div>
	</div>
</t:panel>

<script type="text/javascript">
	function createSpreadsheetData(rowCount, colCount) {
		rowCount = typeof rowCount === 'number' ? rowCount : 100;
		colCount = typeof colCount === 'number' ? colCount : 4;
		var rows = [], i, j;
		for (i = 0; i < rowCount; i++) {
			var row = [];
			for (j = 0; j < colCount; j++) {
				row.push('');
			}
			rows.push(row);
		}
		return rows;
	}
	var $hot=null;
	$(function() {
		initTable();
		$(".panel-body1").height($(window).height()-70);
		$(".wtHolder").height($(window).height()-70);
		reload();
		setTimeout(function(){
			$(".panel-body1").click();	
		},1000);
	});
	
	var empty=function(value, callback){
		setTimeout(function(){
			callback(!$.isNull(value));	
		},10);
	};
	
	
	function initTable(){
		
		var hot_parameter_info = document.getElementById('hot_parameter_info');
		$hot = new Handsontable(hot_parameter_info, {
			rowHeaders : true,
			autoColumnSize:true,
		
			stretchH:'last',
			contextMenu: ['row_above', 'row_below', 'remove_row'],
			colHeaders : [ '货品编号', '货品名称', '规格', "单位", "货品分类", "库存数", "成本价格","采购价格","销售价格","批发价格",
			               "备注","描述","条形码"],
			colWidths:[100,120,100,80,100,100,100,100,100,100,100,100],
			columns: [{data:'cargoNo',validator:empty},
			           {data:'cargoName'},
			           {data:'property'},
			           {data:'unit',validator:empty},
			           {data:'categoryName'},
			           {data:'stockNum',validator:checkDouble},
			           {data:'costPrice',validator:checkDouble},
			           {data:'purchasePrice',validator:checkDouble},
			           {data:'salePrice',validator:checkDouble},
			           {data:'combinationPrice',validator:checkDouble},
			           {data:'comment'},
			           {data:'describes'},
			           {data:'shapeCode'},
			         ]
		});

		$hot.updateSettings({
			contextMenu : {
				items : {
					"row_above" : {name : '在上面添加行'},
					"row_below" : {name : '在下面添加行'},
					"remove_row" : {name : '删除当前行'}
				}
			}
		});
	}
	
	
	function loadTable(){
		return getEmptyData();
	
	}
	
	function getEmptyData() {
		
		var hot_borders_data = createSpreadsheetData(10, 8);
		return hot_borders_data;
	}
	
	function reload(){
		var data=loadTable();
		$hot.loadData(data);
		$hot.render();
	}
	
	function saveForm(){
		
		deleteEmpty();
		
		$hot.validateCells(function(result){
			if(!result){
				$.topNotify("表格有错误信息，检查表格红色区域","error");
				return;
			}
			var parameterInfos=getDataJson();
			var productNoJson=null;
			var cargoNos=getCargoNoJson();
			var cargoNums=cargoNos.join(",");
			var ncargoNo=cargoNos.sort();
			var a=[];
			for (var i = 0; i < cargoNos.length; i++) {
		        if (ncargoNo[i] == ncargoNo[i + 1]) {
		            a.push(ncargoNo[i]);
		        }
		    }
			if(!$.isNull(a)){
				$.topNotify("货品编号"+a.join(",")+"有重复的","error");
				return;
			}
		
			if($.isNull(parameterInfos)){
				$.topNotify("表格无内容","error");
				return;
			}
			var urltest=$.getVirtualPath()+"/cargo/cargo/validate?cargoNos="+cargoNums;
			var str=$.getTextPost(urltest);
			productNoJson =JSON.parse(str);
			if(productNoJson.length==0){
				var url=$.getVirtualPath()+"/cargo/cargo/saves?";
				$.ajax({
					url:url,
					type:'post',
					dataType:'json',
					contentType:"application/json",
					data:JSON.stringify(parameterInfos),
					success:function(result){
						top.main.formSubmit();
						$.topNotify(result.message,result.resultType);	
						top.$("#modal_cargo_form").modal("hide");
						
					}
				});
			}else{
				
				$(".aaa").text("货品编号"+productNoJson+"已存在,请勿重复录入");
			}
			
		});
		
	}
	
	function checkDouble(data,callback){
		var intData=/^[0-9]*[1-9][0-9]*$/;
		var doubleData=/^\d+(\.\d+)?$/;
		if(data!=null){
			if(intData.test(data)||doubleData.test(data)){
				callback(true);
			}else{
				callback(false);
			}
		}else{
			callback(true);
		}
		
	}
	
	function getCargoNoJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push(data.cargoNo);
		}
		
		return columns;
		
	}
	
	function getDataJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push({"cargoNo":data.cargoNo,
						  "cargoName":data.cargoName,
						  "property":data.property,
						  "unit":data.unit,
						  "categoryName":data.categoryName,
						  "stockNum":$.isNull(data.stockNum)?0.0:data.stockNum,
						  "costPrice":$.isNull(data.costPrice)?0.0:data.costPrice,
						  "purchasePrice":$.isNull(data.purchasePrice)?0.0:data.purchasePrice,
						  "salePrice":$.isNull(data.salePrice)?0.0:data.salePrice,
						  "combinationPrice":$.isNull(data.combinationPrice)?0.0:data.combinationPrice,
						  "comment":data.comment,
						  "describes":data.describes,
						  "shapeCode":data.shapeCode
					});
		}
		return columns;
	}
	
	//删除空行
	function deleteEmpty(){
		var rows=$hot.countRows();
		for(var i=rows-1;i>=0;i--){
			if($hot.isEmptyRow(i)){
				$hot.alter('remove_row', i);
			}
		}
	}
	
</script>

<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/handsontable.jsp"%>
<%@ include file="/WEB-INF/views/shared/scripts/editor/ueditor.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/contentFooter.jsp"%>
